package com.zx.sdk.league.member.baidu;

import android.content.Context;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.baidu.mobads.sdk.api.CPUWebAdRequestParam;
import com.baidu.mobads.sdk.api.CpuAdView;
import com.baidu.mobads.sdk.api.CpuLpFontSize;
import com.zx.sdk.model.AdInfo;
import com.zx.sdk.view.widget.BaseNewsAdView;

import java.util.Map;
import java.util.UUID;

public class BaiduNewsAdView extends BaseNewsAdView {
    private final CpuAdView mCpuView;

    public BaiduNewsAdView(Context context, AdInfo mAdInfo) {
        super(context, mAdInfo);
        String outerId = UUID.randomUUID().toString()
            .replace("-", "")
            .substring(0, 16);

        CPUWebAdRequestParam cpuWebAdRequestParam = new CPUWebAdRequestParam.Builder()
            .setLpFontSize(CpuLpFontSize.REGULAR)
            .setLpDarkMode(false)
            .setCityIfLocalChannel("北京") //媒体如果选择本地频道，配置该参数
            .setCustomUserId(outerId)
            .setSubChannelId("") // 设置子渠道号 如果没有则不填
            .build();
        mCpuView = new CpuAdView(context, mAdInfo.getMapAppid(), 1022, cpuWebAdRequestParam,
            new CpuAdView.CpuAdViewInternalStatusListener() {
                /**
                 *  网页加载错误
                 * @param message 错误信息
                 */

                public void loadDataError(String message) {
                }

                /**
                 *  广告点击回调
                 */

                public void onAdClick() {
                }

                /**
                 *  广告展现回调
                 * @param impressionAdNums 有效展现数
                 */

                public void onAdImpression(String impressionAdNums) {
                }

                /**
                 *  内容点击回调
                 */

                public void onContentClick() {

                }

                /**
                 *  内容展现回调
                 * @param impressionContentNums 内容展现数量
                 */

                public void onContentImpression(String impressionContentNums) {

                }

                @Override
                public void onLpContentStatus(Map<String, Object> map) {
                    if ("jump".equals(map.get("act")) && !"ad".equals(map.get("type"))) {
                        Object contentId = map.get("contentId");
                        if (contentId != null) {
                            listener.onNewsPageStart(contentId.toString());
                        }
                    }
                }

                @Override
                public void onExitLp() {
                    listener.onBackToHome();
                }
            });
        // 考虑到媒体在 锁屏开关等场景下 有刷新页面的需求，SDK目前将控件的创建与
        // 数据的请求进行拆分，媒体在已创建控件的前提下，可以直接调用下面这行代码来请求或着更新数据
        mCpuView.requestData();
        addView(mCpuView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void backToHome(boolean clearHistory) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 检测广告组件是否需要处理返回按键
        if (mCpuView != null && mCpuView.onKeyBackDown(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onBackPressed() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mCpuView != null) {
            mCpuView.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mCpuView != null) {
            mCpuView.onPause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCpuView != null) {
            mCpuView.onDestroy();
        }
    }
}
